package com.mrjy.common.util;

import it.sauronsoftware.jave.*;

import java.io.File;

/**
 *
 */
public class ConvertVoiceUtil {
    public static void changeToMp3(String sourcePath, String targetPath) {
        File source = new File(sourcePath);
        File target = new File(targetPath);

        AudioAttributes audio = new AudioAttributes();
        Encoder encoder = new Encoder();
        audio.setCodec("libmp3lame");
        EncodingAttributes attrs = new EncodingAttributes();
        attrs.setFormat("mp3");
        attrs.setAudioAttributes(audio);


        try {
            encoder.encode(source, target, attrs);
        } catch (EncoderException e) {
            e.printStackTrace();
        }
    }

    public static long getMP3Duration(String targetPath) {
        File target = new File(targetPath);
        Encoder encoder = new Encoder();
        MultimediaInfo m = null;

        try {
            m = encoder.getInfo(target);
        } catch (EncoderException e) {
            e.printStackTrace();
        }

        long ls = m.getDuration();
        long duration = ls / 1000;
        return duration;
    }
}